let config = require('./API.js')

module.exports = {
	// 上传图片
	uploadImg: function () {
		var _this = this
		return new Promise(function(resolve, reject) {
			uni.chooseImage({
			    count: 9, //默认9
			    success: function (res) {
					uni.showLoading({
						title: '识别中，请稍后',
						mask: true,
					})
					const arr = []
					res.tempFiles.forEach((item, index) => {
						if ((item.size / 1024) < 2048) {
							uni.uploadFile({
								url: config.apiUrl + '/Advertising/imageUpload',
								filePath: item.path,
								name: 'file',
								formData: {
									'file': item,
								},
								success: (data) => {
									arr.push(data.data)
									if (index+1 == res.tempFiles.length) {
										resolve(arr)
									}
								},
								complete: (com) => {
									if (index+1 == res.tempFiles.length) {
										uni.hideLoading()
									}
								}
							})
						} else {
							uni.showToast({
								title: '图片过大，请重新上传',
								icon: 'none'
							})
						}
					})
			    }
			})
		})
	},
	
	// 校验吨数，费用输入框  保留两位小数
	checkInput: function(value) {
		value = value.replace(/[^\d.]/g, '')
		value = value.replace(/^\./g, '')
		value = value.replace(/^0{2,}/g, '0')
		value = value.replace(/\.{2,}/g, '.')
		value = value.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.')
		value = value.replace('-', '$#$').replace(/\-/g, '').replace('$#$', '-')
		value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
		if ((value.charAt(0) == 0) && (parseInt(value) >= 1)) {
			return value.substring(1, value.length)
		}else{
			return value
		}
	},
	
	// 是否有权限  参数为当前权限
	isAuth: function(auth) {
		return JSON.parse(uni.getStorageSync('permission') || '[]').indexOf(auth) > -1 || false
	},
	
	// 金额用逗号隔开
	numFormat(num) {
		var c = (num.toString().indexOf ('.') !== -1) ? num.toLocaleString() : num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
		return c;
	}
	
}